package com.wizardOther.Items;

import Validator.Validator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.EmployeesModel;
import com.table.EmployeeTableController;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import com.wizardOther.OtherScreensFramework;
import java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 *
 * @author Vi
 */
public class AddNewEmployeeController extends ValidationForm implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;
    @FXML
    private Button add;
    @FXML
    private Button cancel;
    @FXML
    private Button edit;
    @FXML
    private TextField name;
    @FXML
    private TextField surname;
    @FXML
    private TextField pesel;
    @FXML
    private TextField city;
    @FXML
    private TextField postCode;
    @FXML
    private TextField street;
    @FXML
    private TextField house;
    @FXML
    private TextField local;
    @FXML
    private TextField startDate;
    @FXML
    private TextField endDate;
    @FXML
    private TextField phone;
    @FXML
    private TextField money;
    @FXML
    private TextField email;
    @FXML
    private TextField password;
    @FXML
    private ComboBox jobType;
    @FXML
    private TextArea comment;
   
    @FXML
    private Label errorName;
    @FXML
    private Label errorSurname;
    @FXML
    private Label errorPesel;
    @FXML
    private Label errorCity;
    @FXML
    private Label errorStreet;
    @FXML
    private Label errorHouse;
    @FXML
    private Label errorLocal;
    @FXML
    private Label errorJobType;
    @FXML
    private Label errorStartDate;
    @FXML
    private Label errorEndDate;
    @FXML
    private Label errorPhone;
    @FXML
    private Label errorEmail;
    @FXML
    private Label errorPassword;
    @FXML
    private Label errorComment;
    @FXML
    private Label errorMoney;
    @FXML
    private Label errorPostCode;
    
    
    
   
    private Integer selectedEmp;
    private EmployeeTableController controller;
    

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.clear();
        inputs.add(name);
        inputs.add(surname);
        inputs.add(pesel);
        inputs.add(city);
        inputs.add(postCode);
        inputs.add(street);
        inputs.add(house);
        inputs.add(local);
        inputs.add(jobType);
        inputs.add(startDate);
        inputs.add(endDate);
        inputs.add(phone);
        inputs.add(money);
        inputs.add(email);
        inputs.add(password);
        inputs.add(comment);

        labels.clear();
        labels.add(errorName);
        labels.add(errorSurname);
        labels.add(errorPesel);
        labels.add(errorCity);
        labels.add(errorPostCode);
        labels.add(errorStreet);
        labels.add(errorHouse);
        labels.add(errorLocal);
        labels.add(errorJobType);
        labels.add(errorStartDate);
        labels.add(errorEndDate);
        labels.add(errorPhone);
        labels.add(errorMoney);
        labels.add(errorEmail);
        labels.add(errorPassword);
        labels.add(errorComment);
    }
    
    private void clearForm(){
        this.selectedEmp=null;
        this.name.setText("");
        this.surname.setText("");
        this.pesel.setText("");
        this.city.setText("");
        this.postCode.setText("");
        this.street.setText("");
        this.house.setText("");
        this.local.setText("");
        this.jobType.setValue("Wybierz rodzaj pracy");
        this.startDate.setText("");
        this.endDate.setText("");
        this.phone.setText("");
        this.email.setText("");
        this.password.setText("");
        this.comment.setText("");
        this.money.setText("");
        this.postCode.setText("");
        
          makeList(16);
         setWrongTextFields(correctField);
    }
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        if(event.getSource()==add){
            validateForm();
        }
        if(event.getSource()==cancel){
            clearForm();
        } 
        if(event.getSource()==edit){
            showEmpTableDialog();
            
           selectedEmp=controller.getSelectedEmp();
            
            if(this.selectedEmp!=null){
                loadData(selectedEmp);
            }
            
        }
    }
    
     private void loadData(Integer selectedEmp) {
         EmployeesModel emp = DaoInstanceEnum.INSTANCE.getDao().getEmployeeById(selectedEmp);
        if (emp != null) {
                this.name.setText(emp.getFirstname());
                this.surname.setText(emp.getSurname());
                this.pesel.setText(emp.getPeselNumber());
                this.city.setText(emp.getCity());
                this.postCode.setText(emp.getZipCode());
                this.street.setText(emp.getStreet());
                this.house.setText(emp.getHouseNumber());
                if(emp.getLocalNumber()!=null){
                    this.local.setText(emp.getLocalNumber().toString());     
                }
                this.jobType.setValue(emp.getJobType());
                this.startDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(emp.getEmploymentDate()));
                this.endDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(emp.getReleaseDate()));
                this.phone.setText(emp.getPhone());
                this.email.setText(emp.getEmail());
                this.password.setText(emp.getPassword());
                this.comment.setText(emp.getComments());
                this.money.setText(String.valueOf(emp.getSalary()));
                this.postCode.setText(emp.getZipCode());
        }
    }
    
   
    private void validateForm(){
        makeList(16);
        takeDateFromForm();
        setWrongTextFields(correctField);
        
        
        
        printValidator(correctField);
        if(isCorrect(correctField)){
          
            List<Boolean> goodRecord = Validator.checkEmployeeCheckList(getEmployee());
            setWrongTextFields(goodRecord);
            
            if(isCorrect(goodRecord)){
             
               try{
                    DAO dao = DaoInstanceEnum.INSTANCE.getDao();
                    if(selectedEmp!=null){
                        EmployeesModel empl = getEmployee();
                        empl.setEmployeeId(selectedEmp);
                        dao.updateEmployee(empl);
                        createPopup("Zapisano zmiany!");
                    }
                    else{
                        dao.addEmployee(getEmployee());
                        createPopup("Zapisano!");  
                        this.selectedEmp=null;
                    }
                    clearForm();
              }
              catch(Exception e){
                   e.printStackTrace();
              }
            }
        }
        
    }
    
    private void takeDateFromForm(){
        
        
        checkString(this.name.getText(),0);
        checkString(this.surname.getText(),1);
        checkPesel(this.pesel.getText(),2);
        checkString(this.city.getText(),3);
        checkString(this.postCode.getText(),4);
        checkString(this.street.getText(),5);
        checkString(this.house.getText(),6);
        checkInteger(this.local.getText(),7);
        System.out.println("biore z combobox " + this.jobType.getValue());
        checkString(renameJobType((String)this.jobType.getValue()),8);
        checkDate(this.startDate.getText(),9);
        checkDate(this.endDate.getText(), 10);
        checkString(this.phone.getText(),11);
        checkDouble(this.money.getText(),12);
        checkEmail(this.email.getText(),13);
        checkString(this.password.getText(),14);
        checkComments(this.comment.getText(),15);
    }
    
   
    
    @FXML
    private void goToMain(java.awt.event.ActionEvent event) {
        myController.setScreen(OtherScreensFramework.OTHER_BUTTONS_PANEL);
    }
    
    private EmployeesModel getEmployee() {
        
        String name = this.name.getText();
        String surname = this.surname.getText();
        String pesel = this.pesel.getText();
        String city = this.city.getText();
        String post = this.postCode.getText();
        String street = this.street.getText();
        String house = this.house.getText();
        String local = this.local.getText();
        System.out.println("przed "+this.jobType.getValue());
        String job = renameJobType((String)this.jobType.getValue());
        System.out.println("po "+job);
        
        String start = this.startDate.getText();
        String end = this.endDate.getText();
        String phone = this.phone.getText();
        String money = this.money.getText();
        String email = this.email.getText();
        String password = this.password.getText();
        String comments = this.comment.getText();
        
   
        EmployeesModel emp = new EmployeesModel(name, surname, job, pesel, city, post, 
                street, house, Integer.valueOf(local) , Date.valueOf(start), Date.valueOf(end),  Double.valueOf(money), password, email, phone);
           emp.setComments(comments);

          return emp;
   }
   
    
    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
    }

    private boolean showEmpTableDialog() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("EmployeeTable.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz pracownika");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            controller = loader.getController();
            controller.setDialogStage(dialogStage);
           // controller.setWizard(this);

            dialogStage.showAndWait();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

   
}
